/********************************************************************************
 *      Copyright:  (C) 2022 YanlinZhou<1222>
 *                  All rights reserved.
 *
 *       Filename:  main.h
 *    Description:  This head file 
 *
 *        Version:  1.0.0(04/10/2022)
 *         Author:  YanlinZhou <1222>
 *      ChangeLog:  1, Release initial version on "04/10/2022 08:58:56 PM"
 *                 
 ********************************************************************************/

#ifndef  _MAIN_H_
#define  _MAIN_H_

#include <time.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <unistd.h>

#include "sqlite3.h"
#include "sqlite.h"
#include "socket.h"
#include "ds18b20.h"
#include "debug.h"

struct my_time
{
    int         sample_interval;
    long int    current_time;
    long int    last_time;
    time_t      tv; 
};

/*********************************************************************
 FunctionName: determin_time
 Function: Determine whether the sampling time is reached
 Parameters: @struct my_time *t
 Return: return 1 if ok, else return 0 
 Notes:
 *********************************************************************/
int determin_time(struct my_time *t);


/*********************************************************************
 FunctionName: if_daemon(int sw)
 Function: Whether to enable background running
 Parameters: sw is 1 or 0
 Return: return 1 if ok, else return -1 
 Notes:
 *********************************************************************/
int if_daemon(int sw);


/*********************************************************************
 FunctionName: sig_handle(int signum)
 Function: Program end prompt
 Parameters: @int signum
 Return: NULL 
 Notes:
 *********************************************************************/
void sig_handle(int signum);


/*********************************************************************
 FunctionName: handle_pipe
 Function: let GIGPIPE do nothing
 Parameters: @int signal number
 Return: return 1 if ok, else return 0 
 Notes:
 *********************************************************************/
void handle_pipe(int sig);


/*********************************************************************
 FunctionName: send_sample_data_if_failure_save_to_db(socket_info_t *sokt, sqlite_info_t sqlite_info, char *str, float temperature, char *time)
 Function: send_sample_data_if_failure_save_to_db
 Parameters: 
 Return: return 1 if ok, return -1 if send data failure, else ruturn -2 if save data to database failure 
 Notes:
 *********************************************************************/
int send_sample_data_if_failure_save_to_db(socket_info_t *sokt, sqlite_info_t sqlite_info, char *str, float temperature, char *time);

#endif   /* ----- #ifndef _MAIN_H_  ----- */

